package com.note.chh.notebook.UI.activity;


import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.NetworkUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.MainActivity;
import com.note.chh.notebook.Model.BeautifulPictureModel;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.Uitls.UserUtil;
import com.note.chh.notebook.Uitls.gson.GsonImpl;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;

//闪屏页面
public class GuideActivity extends BaseActivity {

    @BindView(R.id.iv_splash_ad)
    ImageView ivSplashAd;
    @BindView(R.id.start_skip_count_down)
    TextView startSkipCountDown;
    private MyCountDownTimer mCountDownTimer;
    private List<Integer> bgList;
    @Override
    protected void onDestroy() {
        //显示状态栏
        statusBarVisible(true);

        //界面销毁的时候将 CountDownTimer 销毁
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
        super.onDestroy();
    }

    /**
     * 屏蔽返回键
     */
   @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void initView() {
        //隐藏状态栏
        statusBarVisible(false);
        //沉浸状态栏
//        QMUIStatusBarHelper.translucent(GuideActivity.this);

        startSkipCountDown.setText(/*Constant.SPLASH_TIME+"s */"跳过");
        //创建倒计时类
        mCountDownTimer = new MyCountDownTimer(Constant.SPLASH_TIME*1000+1100, 1000);

        getSPlist();


    }


    @Override
    public void initData() {
//        if (bgList == null) {
//            bgList = new ArrayList<>();
//            bgList.add(R.mipmap.b_music_bg);
//            bgList.add(R.mipmap.b_music_bg_a);
//            bgList.add(R.mipmap.b_music_bg_b);
//            bgList.add(R.mipmap.b_music_bg_c);
//            bgList.add(R.mipmap.pic_page_background);
//        }
//        Random random = new Random();
//        int i = random.nextInt(5);
//        ivSplashAd.setImageResource(i<5?bgList.get(i):bgList.get(0));
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_guide;
    }

    @OnClick(R.id.start_skip_count_down)
    public void onViewClicked() {
        startActivity(MainActivity.class);
        overridePendingTransition(R.anim.screen_zoom_in, R.anim.screen_zoom_out);
        finish();
    }




    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture
         *      表示以「 毫秒 」为单位倒计时的总数
         *      例如 millisInFuture = 1000 表示1秒
         *
         * @param countDownInterval
         *      表示 间隔 多少微秒 调用一次 onTick()
         *      例如: countDownInterval = 1000 ; 表示每 1000 毫秒调用一次 onTick()
         *
         */

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }


        public void onFinish() {
//            startSkipCountDown.setText("0s 跳过");
            startActivity(MainActivity.class);
            overridePendingTransition(R.anim.screen_zoom_in, R.anim.screen_zoom_out);
            finish();
        }

        public void onTick(long millisUntilFinished) {
//            UserUtil.dLog("millisUntilFinished_1="+millisUntilFinished);
//            UserUtil.dLog("millisUntilFinished_2="+(int)Math.rint(millisUntilFinished/ 1000));
            startSkipCountDown.setText( /*(int)Math.rint(millisUntilFinished/ 1000)-1 + "s */"跳过");

        }
    }


    private void getSPlist() {
        Random random = new Random();
        int i = random.nextInt(100);
        OkGo.<String>get("http://gank.io/api/data/福利/" + 1 + "/" + i)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                UserUtil.dLog(" response = "+response.body());
                        BeautifulPictureModel beautifulPictureModel = GsonImpl.get().toObject(response.body(), BeautifulPictureModel.class);
                        List<BeautifulPictureModel.ResultsBean> results = beautifulPictureModel.getResults();

                        RequestOptions options = new RequestOptions();
                        options.centerCrop().error(R.mipmap.pic_page_background);
                        Glide.with(GuideActivity.this).load(results.get(0).getUrl()).apply(options).into(ivSplashAd);

                        mCountDownTimer.start();
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        mCountDownTimer.start();
                        ivSplashAd.setImageResource(R.mipmap.pic_page_background);
                        //网络不通
                        if (!NetworkUtils.isAvailableByPing()) {
//                            showToast(GuideActivity.this,Constant.NETWORKNOUSER);
                            Toast.makeText(GuideActivity.this,Constant.NETWORKNOUSER,Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

}
